/*    */ package com.zimbra.cs.security.sasl;
/*    */ 
/*    */ import java.io.DataOutputStream;
/*    */ import java.io.IOException;
/*    */ import java.io.OutputStream;
/*    */ import java.nio.ByteBuffer;
/*    */ import javax.security.sasl.SaslClient;
/*    */ import javax.security.sasl.SaslServer;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class SaslOutputStream
/*    */   extends OutputStream
/*    */ {
/*    */   private final DataOutputStream mOutputStream;
/*    */   private final SaslSecurityLayer mSecurityLayer;
/*    */   private final SaslOutputBuffer mBuffer;
/*    */   private static final boolean DEBUG = false;
/*    */   
/*    */   public SaslOutputStream(OutputStream os, SaslServer server)
/*    */   {
/* 35 */     this(os, SaslSecurityLayer.getInstance(server));
/*    */   }
/*    */   
/*    */   public SaslOutputStream(OutputStream os, SaslClient client) {
/* 39 */     this(os, SaslSecurityLayer.getInstance(client));
/*    */   }
/*    */   
/*    */   public SaslOutputStream(OutputStream os, SaslSecurityLayer securityLayer) {
/* 43 */     this.mOutputStream = new DataOutputStream(os);
/* 44 */     this.mSecurityLayer = securityLayer;
/* 45 */     this.mBuffer = new SaslOutputBuffer(securityLayer.getMaxSendSize());
/*    */   }
/*    */   
/*    */   public void write(byte[] b, int off, int len) throws IOException {
/* 49 */     debug("write: enter len = %d", new Object[] { Integer.valueOf(len) });
/* 50 */     if ((off | len | off + len | b.length - (off + len)) < 0)
/* 51 */       throw new IndexOutOfBoundsException();
/* 52 */     if (len == 0) {
/* 53 */       return;
/*    */     }
/* 55 */     for (int count = 0; count < len;) {
/* 56 */       int n = writeBytes(b, off, len - count);
/* 57 */       count += n;
/* 58 */       off += n;
/* 59 */       debug("write: loop n = %d, count = %d", new Object[] { Integer.valueOf(n), Integer.valueOf(count) });
/*    */     }
/* 61 */     debug("write: exit", new Object[0]);
/*    */   }
/*    */   
/*    */   public void write(int b) throws IOException {
/* 65 */     debug("write: enter b = %d", new Object[] { Integer.valueOf(b) });
/* 66 */     ensureBuffer();
/* 67 */     this.mBuffer.put((byte)b);
/*    */   }
/*    */   
/*    */   private int writeBytes(byte[] b, int off, int len) throws IOException {
/* 71 */     ensureBuffer();
/* 72 */     ByteBuffer bb = ByteBuffer.wrap(b, off, len);
/* 73 */     this.mBuffer.put(bb);
/* 74 */     return bb.position();
/*    */   }
/*    */   
/*    */   private void ensureBuffer() throws IOException {
/* 78 */     if (this.mBuffer.isFull()) flushBuffer();
/*    */   }
/*    */   
/*    */   private void flushBuffer() throws IOException {
/* 82 */     byte[] b = this.mBuffer.wrap(this.mSecurityLayer);
/* 83 */     this.mOutputStream.writeInt(b.length);
/* 84 */     this.mOutputStream.write(b);
/* 85 */     this.mBuffer.clear();
/*    */   }
/*    */   
/*    */   public void flush() throws IOException
/*    */   {
/* 90 */     if (this.mBuffer.size() > 0) flushBuffer();
/* 91 */     this.mOutputStream.flush();
/*    */   }
/*    */   
/*    */   public void close() throws IOException {
/* 95 */     flush();
/* 96 */     this.mOutputStream.close();
/*    */   }
/*    */   
/*    */   private static void debug(String format, Object... args) {}
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/security/sasl/SaslOutputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */